// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: qjs_shadow_root.cpp
// Description: Bindings for ShadowRoot
//      Author: Ziming Li
//     Created: 2022-04-11
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./qjs_bindings.h"

#include "blinkit/blink/renderer/bindings/core/qjs/qjs.h"
#include "blinkit/blink/renderer/bindings/core/qjs/qjs_c_function_list_entries.h"
#include "blinkit/blink/renderer/bindings/core/script_controller.h"
#include "blinkit/blink/renderer/core/css/style_sheet_list.h"
#include "blinkit/blink/renderer/core/dom/shadow/ShadowRoot.h"

/**
 * https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot
 */

namespace BlinKit {
namespace qjs {

static JSValue GetHost(JSContext *ctx, JSValueConst thisVal)
{
    ShadowRoot *shadowRoot = ScriptWrappable::Cast<ShadowRoot>(thisVal);
    return ScriptController::From(ctx)->WrapElement(shadowRoot->host());
}

static JSValue GetStyleSheets(JSContext *ctx, JSValueConst thisVal)
{
    ShadowRoot *shadowRoot = ScriptWrappable::Cast<ShadowRoot>(thisVal);
    return ScriptController::From(ctx)->Wrap(shadowRoot->styleSheets());
}

JSValue CreateShadowRootPrototype(JSContext *ctx, JSValue documentFragmentPrototype)
{
    static const JSCFunctionListEntry Funcs[] = {
        CGetSetEntry("host", GetHost, nullptr),
        CGetSetEntry(Strings::StyleSheets, GetStyleSheets, nullptr)
    };

    JSValue ret = JS_NewObjectProto(ctx, documentFragmentPrototype);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}

} // namespace qjs
} // namespace BlinKit
